﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ParkingLot.Entitys;
using ParkingLot.Entitys.Enums;
using ParkingLot.Entitys.Exceptions;
using ParkingLot.Utility.Factory;

namespace ParkingLot.Utility.DBConnection.SqlSever
{
    /// <summary>
    /// SqlSever常连接(不可用于多线程)
    /// </summary>
    public class SqlDbSession
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        public static readonly string ConnectionString = ConfigManager.AppSettings["ConnectionString"].Value;

        private static SqlTransaction m_SqlTransaction;

        private static SqlConnection m_Con;
        public static SqlConnection Con
        {
            get
            {
                if (m_Con == null)
                {
                    m_Con = new SqlConnection(ConnectionString);
                    m_Con.Open();
                }
                else if (m_Con.State == ConnectionState.Closed)
                {
                    m_Con.Open();
                }
                return m_Con;
            }
        }

        #region 事务

        /// <summary>
        /// 开始事务
        /// </summary>
        public static void BeginTransaction(out Prompt prompt)
        {
            prompt = new Prompt();
            try
            {
                if (m_SqlTransaction != null)
                {
                    prompt.Message = "尚有未提交或者未回滚的数据库";
                    prompt.ExceptionType = ExceptionType.Wrong;
                    return;
                }
                m_SqlTransaction = m_Con.BeginTransaction();

            }
            catch (Exception exception)
            {
                prompt.Message = exception.Message;
                prompt.ExceptionType = ExceptionType.DbsessionException;
                //throw ExceptionFactory.CreateException(ExceptionType.DbsessionException,exception.Message);
            }
        }

        #endregion
        public static int
             ExecuteCommand(string sql, out Prompt prompt)
        {
            prompt = new Prompt();
            int result=-2;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Con);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                prompt.ExceptionType = ExceptionType.DbsessionException;
                prompt.Message = exception.Message;
            }
            return result;
        }

        public static int ExecuteCommand(string sql, params SqlParameter[] param)
        {
            SqlCommand cmd = new SqlCommand(sql, Con);
            cmd.Parameters.AddRange(param);
            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sqlBuf"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(StringBuilder sqlBuf)
        {
            return GetDataTable(sqlBuf.ToString());
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql)
        {
            SqlDataAdapter dap = new SqlDataAdapter(sql, Con);
            DataSet ds = new DataSet();
            dap.Fill(ds);

            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }
    }
}
